<?php

class DefaultController extends Controller
{
    public $title = '«Автомир Стерлитамака» - Бесплатная газета частных объявлений для автомобилистов';

	public function actionIndex()
	{
        $criteria = new CDbCriteria;
        $criteria->addCondition("type='articles'");
        $criteria->addCondition('is_active=1');
        $criteria->order = 'date_created DESC';
        $count = Article::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $articles = Article::model()->findAll($criteria);
        $this->render('index',
            array(
                'articles' => $articles,
                'pages' => $pages
            )
        );
	}

    public function actionView($id){
        $articles = Article::model()->findByPk($id);
        $this->title = $articles->title.' - '.$this->title;
        $this->render('view', array('articles' => $articles));
    }

    public function actionCategory($id)
    {
        $criteria = new CDbCriteria;
        $criteria->addCondition("type='articles'");
        $criteria->addCondition("category_id=".$id);
        $criteria->addCondition('is_active=1');
        $criteria->order = 'date_created DESC';
        $count = Article::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        $articles = Article::model()->findAll($criteria);
        $this->render('index',
            array(
                'articles' => $articles,
                'pages' => $pages
            )
        );
    }
}